import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:qaun_ni_dev/provider/attributeProvide.dart';
import 'package:qaun_ni_dev/pages/attribute/attribute_add.dart';
import 'package:qaun_ni_dev/pages/attribute/attribute_detail.dart';



class AttributePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('属性列表'),
        actions: <Widget>[
          new IconButton(
            icon: Icon(Icons.add),
            onPressed: () {
              Navigator.push(
                  context, MaterialPageRoute(builder: (context) => AttributeAdd()));
            },
          ),

        ],
      ),
      body: Container(
          child: AttributeListPage()
      ),
    );
  }
}


class AttributeListPage extends StatefulWidget {
  @override
  _AttributeListPage createState() => _AttributeListPage();

}

class _AttributeListPage extends State<AttributeListPage> {

  GlobalKey<RefreshFooterState> _footerKey = new GlobalKey<RefreshFooterState>();
  GlobalKey<RefreshHeaderState> _headerKey = new GlobalKey<RefreshHeaderState>();

  void initState() {
    super.initState();
    _getList();
  }

  void _getList() {
    Provider.of<AttributeProvide>(context,listen: false).getAttributeList(context);
  }

  @override
  Widget build(BuildContext context) {

    return Stack(
      children: <Widget>[
        Positioned(
          top: 0,
          left: 0,
          height: 90,
          child: _inputSearch(context),
        ),

        Container(
          padding: EdgeInsets.only(top: 65),
          child: Column(
            children: <Widget>[
              Expanded(
                flex: 1,
                child: Consumer<AttributeProvide>(
                    builder: (context,data,_){
                      if(data.attributeListData.length >0 ) {
                        return Container(
                          padding: EdgeInsets.only(top: 10,left: 15,right: 15),
                          height: 540,
                          child: EasyRefresh(
                              refreshFooter: ClassicsFooter(
                                  key:_footerKey,
                                  bgColor:Colors.white,
                                  textColor:Colors.pink,
                                  moreInfoColor: Colors.pink,
                                  showMore:true,
                                  noMoreText: '没有更多了',
                                  moreInfo:'加载中',
                                  loadReadyText:'上拉加载'
                              ),
                              refreshHeader:ClassicsHeader(
                                  key: _headerKey,
                                  refreshText: "下拉刷新数据",
                                  refreshReadyText: "准备刷新数据",
                                  refreshingText: "数据刷新中...",
                                  refreshedText: "刷新完成",
                                  bgColor: Colors.blue,
                                  textColor: Colors.white,
                                  moreInfoColor: Colors.white,
                                  refreshHeight: 70.0,
                                  isFloat: false,
                                  showMore: false,
                                  moreInfo: "Updated at %T"
                              ),
                              child: Container(
                                height: 540,
                                padding: EdgeInsets.only(top: 15),
                                child: ListView.builder(
                                    itemCount: data.attributeListData.length,
                                    itemBuilder: (context,index) {
                                      return _listWidget(data.attributeListData,index);
                                    }
                                ),
                              ),
                              loadMore: () async {
                                print('加载更多的');
                                Provider.of<AttributeProvide>(context,listen: false).getAttributeMoreList(context);
                              },
                              onRefresh: () async{
                                print('上拉刷新');
                                Provider.of<AttributeProvide>(context,listen: false).getAttributeList(context);
                              }
                          ),
                        );
                      } else {
                        return Text('暂无数据哦，请稍等');
                      }
                    }
                ),
              ),
            ],
          )
        )
      ],
    );
  }

  Widget _inputSearch(context) {
    TextEditingController code = TextEditingController();
    TextEditingController type = TextEditingController();
    return Container (
      margin: EdgeInsets.only(bottom: 10),
      padding: EdgeInsets.only(top: 10,left: 30,right: 15),
      child: Column(
        children: <Widget>[
          Container(
            child: Row(
              children: <Widget>[
                Column(
                  children: <Widget>[
                    Container(
                      child: TextField(
                        controller: code,
                        autofocus: false,
                        keyboardType: TextInputType.text,
                        decoration: InputDecoration(
                          hintText: '输入查询的code',
                          contentPadding: const EdgeInsets.symmetric(vertical: 8.0,horizontal: 10),
                        ),
                      ),
                      width: 210,
                      height: 30,
                      decoration: BoxDecoration(
                          border: Border.all(width: 1,color: Colors.grey)
                      ),
                      margin: EdgeInsets.only(bottom: 6),
                    ),
                    Container(
                      child: TextField(
                        controller: type,
                        autofocus: false,
                        keyboardType: TextInputType.text,
                        decoration: InputDecoration(
                          hintText: '输入查询的type',
                          contentPadding: const EdgeInsets.symmetric(vertical: 8.0,horizontal: 10),
                        ),
                      ),
                      width: 210,
                      height: 30,
                      decoration: BoxDecoration(
                          border: Border.all(width: 1,color: Colors.grey)
                      ),
                    ),
                  ],
                ),
                Container(
                  padding: EdgeInsets.only(top: 8,left: 25,right: 25,bottom: 8),
                  margin: EdgeInsets.only(left: 20),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(20)),
                    color: Colors.lightBlue,
                  ),
                  child: InkWell(
                    child: Text('搜索',style: TextStyle(
                      color: Colors.white
                    ),
                    ),
                    onTap: () async {
                      await Provider.of<AttributeProvide>(context,listen: false).changeGetSearch(code.text, type.text);
                      code.text = '';
                      type.text = '';
                      await Provider.of<AttributeProvide>(context,listen: false).getAttributeList(context);
                    },
                  )
                )
              ],
            ),
          ),
        ],
      ),
    );
  }








  Widget _listWidget (List newList,int index) {
    return InkWell(
      onTap: () {
        Navigator.push(context, MaterialPageRoute(
            builder: (context) {
              return AttributeDetail(newList[index]);
            }
        ));
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 10),
        decoration: BoxDecoration(
            border: Border.all(width: 1,color: Colors.black12),
            borderRadius: BorderRadius.all(Radius.circular(6))
        ),
        child: Row(
          children: <Widget>[
            _leftOrderNumber(newList,index),
            _rightContent(newList,index),
          ],
        ),
      ),
    );
  }

  Widget _leftOrderNumber(newList,index) {
    return Container(
      margin: EdgeInsets.only(right: 10),
      padding: EdgeInsets.only(left: 6),
      width: 50,
      child: Column(
        children: <Widget>[
          Text('序号${index}',
            textAlign: TextAlign.center,

          ),
        ],
      ),
    );
  }

  Widget _rightContent(newList,index) {
    return Container(
      padding: EdgeInsets.only(left: 8,right: 6,top: 6,bottom: 6),
      decoration: BoxDecoration(
          border: Border(
            left: BorderSide(color: Colors.grey,width: 1),
          )
      ),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '属性code:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${newList[index].code}'),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '属性val:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${newList[index].val}'),
              ),
            ],
          ),

          Row(
            children: <Widget>[
              Container(
                width: 70,
                child: Text(
                  '属性类型:',
                ),
              ),
              Container(
                width: 160,
                child: Text('${newList[index].type}'),
              ),
            ],
          ),
        ],
      ),
    );
  }


}



